package action;

import java.io.File;
import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import model.FileModel;
import model.User;
import org.apache.commons.fileupload.FileUpload;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.interceptor.ParameterAware;
import org.apache.struts2.interceptor.ServletRequestAware;
import service.FileService;

/**
 * Created by IntelliJ IDEA.
 * User: TM
 * Date: 11/17/12
 * Time: 11:27 PM
 * To change this template use FileModel | Settings | FileModel Templates.
 */
public class FileUploadAction extends ActionSupport implements ParameterAware {

    private File fileUpload;
    private String fileUploadContentType;
    private String fileUploadFileName;
    private String description;
    private FileService fileService;
    private List<FileModel> fileModelList;
    private Map<String, String[]> parameter;
    private String path;
    private int imageId;

    public String getFileUploadContentType() {
        return fileUploadContentType;
    }

    public void setFileUploadContentType(String fileUploadContentType) {
        this.fileUploadContentType = fileUploadContentType;
    }

    public String getFileUploadFileName() {
        return fileUploadFileName;
    }

    public void setFileUploadFileName(String fileUploadFileName) {
        this.fileUploadFileName = fileUploadFileName;
    }

    public File getFileUpload() {
        return fileUpload;
    }

    public void setFileUpload(File fileUpload) {
        this.fileUpload = fileUpload;
    }

    public String execute() throws Exception {
        try {
            ActionContext ctx = ActionContext.getContext();
            Map session = ctx.getSession();
            User user = (User) session.get("USER");
            String filePath = "c:/Myuploads/" + user.getNic();  // Path where uploaded file will be stored
            System.out.println("Server path:" + filePath); // check your path in console
            File fileToCreate = new File(filePath, fileUploadFileName);// Create file name  same as original
            FileUtils.copyFile(fileUpload, fileToCreate); // Just copy temp file content tos this file

            FileModel fileUpload = new FileModel();
            fileUpload.setPath(filePath + "/" + fileUploadFileName);
            fileUpload.setDescription(getDescription());
            fileService.uploadFile(user, fileUpload);

        } catch (Exception e) {
            e.printStackTrace();
            addActionError(e.getMessage());
            return INPUT;

        }
        return SUCCESS;

    }

    public String fileList() {
        ActionContext ctx = ActionContext.getContext();
        Map session = ctx.getSession();
        User user = (User) session.get("USER");
        fileModelList = fileService.getFileList(user);
        return "displayFileList";
    }

    public String displayReport() {
        ActionContext ctx = ActionContext.getContext();
        Map session = ctx.getSession();
        session.put("ImageId", parameter.get("uploadId")[0]);

        return "displayReport";
    }



    public String display() {
        return NONE;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public FileService getFileService() {
        return fileService;
    }

    public void setFileService(FileService fileService) {
        this.fileService = fileService;
    }

    public List<FileModel> getFileModelList() {
        return fileModelList;
    }

    public void setFileModelList(List<FileModel> fileModelList) {
        this.fileModelList = fileModelList;
    }

    public void setParameters(Map<String, String[]> stringMap) {
        this.parameter =  stringMap;
    }

    public Map<String, String[]> getParameter() {
        return parameter;
    }

    public void setParameter(Map<String, String[]> parameter) {
        this.parameter = parameter;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public int getImageId() {
        return imageId;
    }

    public void setImageId(int imageId) {
        this.imageId = imageId;
    }
}